#include <baker/BasicOccluder.h>
#include <baker/HitState.h>
#include <unittest++/UnitTest++.h>

using namespace baker;

SUITE( TestBasicOccluder )
{
    void initOccluder( BasicOccluder & occluder )
    {
        // face 0
        occluder.addVertex( Vec3( 0, 1, 1 ) );
        occluder.addVertex( Vec3( 1, 1, 0 ) );
        occluder.addVertex( Vec3(-1, 1, 0 ) );

        occluder.addFace( 0, 1, 2 );

        // face 1
        occluder.addVertex( Vec3( 0, 2, 1 ) );
        occluder.addVertex( Vec3( 1, 2, 0 ) );
        occluder.addVertex( Vec3(-1, 2, 0 ) );

        occluder.addFace( 3, 4, 5 );

        // face 2
        occluder.addVertex( Vec3( 0, 3, 1 ) );
        occluder.addVertex( Vec3( 1, 3, 0 ) );
        occluder.addVertex( Vec3(-1, 3, 0 ) );

        occluder.addFace( 6, 7, 8 );

        occluder.build();
    }

    TEST( hitTestFast )
    {
        BasicOccluder occluder;

        Ray3 ray( Vec3( 0, 0, 0 )
                , Vec3( 0, 1, 0 )
                );

        initOccluder( occluder );

        CHECK( occluder.hitTestFast( ray ) );
    }

    TEST( hitTestFull )
    {
        BasicOccluder occluder;

        Ray3 ray( Vec3( 0, 0, 0 )
                , Vec3( 0, 1, 0 )
                );

        initOccluder( occluder );

        HitState hitState;

        occluder.hitTestFull( hitState, ray );

        CHECK( hitState.getHitCount() == 3 );

        CHECK( hitState.getClosestHitIndex() == 0 );

        CHECK_CLOSE( hitState.getHitInfo( 0 ).dist, 1.0, EPSILON );
        CHECK_CLOSE( hitState.getHitInfo( 1 ).dist, 2.0, EPSILON );
        CHECK_CLOSE( hitState.getHitInfo( 2 ).dist, 3.0, EPSILON );
    }
}
